package com.changgou.core.controller;

import com.alibaba.fastjson.JSON;
import com.changgou.core.service.CoreService;
import com.changgou.entity.Result;
import com.changgou.entity.StatusCode;
import com.github.pagehelper.PageInfo;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.context.request.WebRequestInterceptor;

import java.util.List;
import java.util.Map;

/**
 * @author liyipeng
 * @date 2021-06-27 11:17
 */
public abstract class AbstractCoreController<T> implements ICoreController<T>  , WebRequestInterceptor {

    /**
     * 调用方的service
     */
    protected CoreService<T> coreService;
    /**
     * 调用方的类型
     */
    protected Class<T> clazz;


    protected Map<String, String> map;

    public AbstractCoreController(CoreService<T> coreService, Class<T> clazz) {
        this.coreService = coreService;
        this.clazz = clazz;
    }

    @DeleteMapping("/{id}")
    @Override
    public Result deleteById(@PathVariable(name = "id") Object id) {
        coreService.deleteById(id);
        return new Result(true, StatusCode.OK, "删除成功");
    }

    @DeleteMapping("delete")
    @Override
    public Result delete(@RequestBody T record) {
        coreService.delete(record);
        return new Result(true, StatusCode.OK, "删除成功");
    }

    @PostMapping
    @Override
    public Result insert(@RequestBody T record) {
        coreService.insert(record);
        return new Result(true, StatusCode.OK, "添加成功");
    }

    @GetMapping(value = "/search/{page}/{size}")
    @Override
    public Result<PageInfo<T>> findByPage(@PathVariable(name = "page") Integer pageNo,
                                          @PathVariable(name = "size") Integer pageSize) {
        PageInfo<T> pageInfo  = coreService.findByPage(pageNo, pageSize);
        return new Result(true, StatusCode.OK, "分页查询成功",pageInfo);
    }


    @PostMapping(value = "/search/{page}/{size}")
    @Override
    public Result<PageInfo<T>> findByPage(@PathVariable(name = "page") Integer pageNo,
                                          @PathVariable(name = "size") Integer pageSize,
                                          @RequestBody(required = false) T record) {
        PageInfo<T> pageInfo = coreService.findByPage(pageNo, pageSize, record);
        return new Result(true, StatusCode.OK, "条件分页查询成功",pageInfo);
    }


    @GetMapping
    @Override
    public Result<List<T>> findAll() {
        List<T> list = coreService.selectAll();
        return new Result(true, StatusCode.OK, "查询成功",list);
    }

    @GetMapping("{id}")
    @Override
    public Result<T> findById(@PathVariable(name = "id") Object id) {
        T t = coreService.selectByPrimaryKey(id);
        return new Result<T>(true, StatusCode.OK, "查询单个数据成功", t);
    }

    @PostMapping("/search")
    @Override
    public Result<List<T>> find(@RequestBody T record) {
        List<T> list = coreService.select(record);
        return new Result(true, StatusCode.OK, "根据条件查询成功",list);
    }

    @PutMapping
    @Override
    public Result update(@RequestBody T record) {
        coreService.updateByPrimaryKey(record);
        return new Result(true, StatusCode.OK, "更新成功");
    }


    private static final String AUTHORIZE_TOKEN_HEADER = "Authorization";
    @Override
    public void preHandle(WebRequest request) throws Exception {
        //处理器执行之前 执行
        /*ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        if (requestAttributes != null) {
            //获取登录的用户名 value 是一个字符串{“username_name”:"zhangsna"。。。。。。。}
            String value = requestAttributes.getRequest().getHeader(AUTHORIZE_TOKEN_HEADER);
            if (!StringUtils.isEmpty(value)) {
                map = JSON.parseObject(value, Map.class);
            }
        }*/

        String value =request.getHeader(AUTHORIZE_TOKEN_HEADER);
        if (!StringUtils.isEmpty(value)) {
            map = JSON.parseObject(value, Map.class);
        }

    }

    @Override
    public void postHandle(WebRequest request, ModelMap model) throws Exception {

        //处理器中执行
    }

    @Override
    public void afterCompletion(WebRequest request, Exception ex) throws Exception {

        //处理器执行之后执行
    }


}
